package com.example.a10928.weifriend.http.configuration;


import com.example.a10928.weifriend.data.User;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.a10928.weifriend.App.app;


/**
 * Created by ipcjs on 2017/05/08.
 */
public class InsertSignInterceptor implements Interceptor {
    /**
     * FormUrlEncoded 的接口必须包含一个Field, 否则报错;
     * 如果需要的参数都能由该类自动插入, 则接口定义时添加名为{@link #FIELD_EMPTY}的String参数, 防止报错;
     * 使用时传入值: @{@link #FIELD_EMPTY_VALUE}
     */
    public static final String FIELD_EMPTY = "__empty_field__";
    public static final String FIELD_EMPTY_VALUE = "__empty_field_value__";
    private static final String HEADER_NAME = "Authorization";
    /**
     * 用于标记出需要插入token的API
     */
    public static final String HEADER = HEADER_NAME + ": 1";

    private static FormBody.Builder newFormBodyBuilderAndFilterEmpty(FormBody formBody) {
        FormBody.Builder builder = new FormBody.Builder();
        for (int i = 0; i < formBody.size(); i++) {
            if (!FIELD_EMPTY.equals(formBody.name(i))) { // FIELD_EMPTY是防止报错的占位字段, 不需要copy
                builder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i));
            }
        }
        return builder;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (request.header(HEADER_NAME) != null) {
            User instance = app().getUser();
//            Log.v("token:", "" + instance.getToken());
            if ("GET".equals(request.method())) {
                HttpUrl url = request.url().newBuilder()
                        .build();
                request = request.newBuilder()
                        .removeHeader(HEADER_NAME)
                        .addHeader(HEADER_NAME, instance.getToken())
                        .url(url)
                        .build();
            } else if ("POST".equals(request.method())) {
                if (request.body() instanceof FormBody) { // post请求, 只能处理body为FormBody的情况
                    FormBody formBody = newFormBodyBuilderAndFilterEmpty((FormBody) request.body())
                            .build();
                    request = request.newBuilder()
                            .removeHeader(HEADER_NAME)
                            .addHeader(HEADER_NAME, instance.getToken())
                            .post(formBody)
                            .build();
                }else  if (request.body() instanceof MultipartBody) { // post请求,
                    request = request.newBuilder()
                            .removeHeader(HEADER_NAME)
                            .addHeader(HEADER_NAME, instance.getToken())
                            .post(request.body())
                            .build();
                }
            }

        }
        return chain.proceed(request);
    }
}
